/******************************************************************************************
  This is to plot the moments and profiles from the NoDepAtWork models
  in the same graph.

  in their own folder, 

  ** dta_MSM_Moments.dta: moments
        _f: generate from Fortram program
        _d: from SIPP data
       lfpr lnw sd_lnw lnw_fd lfpr_diff
*******************************************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

local lvparfile1 "../../v19/gadi.nodepatwork/gadi.nodepatwork.v7.txt";
local lvparfile2 "../gadi.nodepatwork2/gadi.nodepatwork2.v5.txt";

local lvdtafile1 "../../v19/gadi.nodepatwork/gadi.nodepatwork.v7/dta_MSM_ProfilesMoments.dta";
local lvdtafile2 "../gadi.nodepatwork2/gadi.nodepatwork2.v5/dta_MSM_ProfilesMoments.dta";

global gpicsdir "pics_NoDepAtWork";
global gdtadir "data";

*log using log_NoDepAtWork.log, replace;

include do_globals.do;

*************************************************************************;
insheet using pars_mapping.csv, comma names clear;
sort incode;
save ${gdtadir}/dta_temp.dta, replace;

* get parameter estimates;
foreach iv in 1 2 {;
   infile par using `lvparfile`iv'', clear;
   keep if [_n]<=${gvparnum};
   gen incode = [_n];
   destring par, replace;
   prog_parSSAx1000 par;

   gen byte model = `iv';
   if (`iv' > 1) {;
      append using ${gdtadir}/dta_NoDepAtWork_pars.dta;
   };
   sort incode model;
   save ${gdtadir}/dta_NoDepAtWork_pars.dta, replace;
};
merge m:1 incode using ${gdtadir}/dta_temp.dta;
keep if _merge==3;
drop _merge;

sort inpaper model;
save ${gdtadir}/dta_NoDepAtWork_pars.dta, replace;

reshape wide par, i(inpaper) j(model);
sort inpaper; 

local lvpars par1 par2;
export excel inpaper description `lvpars' incode using "table4paper_NoDepAtWork", sheet("pars_NoDepAtWork") sheetreplace firstrow(variables);

local lvsdms sdH sdA sdinc p50A A lnH lnH_worker I I_cond;
*************************************************************************;
* import moments for comparison;
foreach iv in 1 2 {;
   use `lvdtafile`iv'' if ihet==0, clear;
   keep  t *_f `lvsdms';
   ren *_f *; 
   sort t;
   gen byte model = `iv';
   if (`iv' > 1) {;
      append using ${gdtadir}/dta_MSM_Moments_NoDepAtWork_compare.dta;
   };
   sort model t;
   save ${gdtadir}/dta_MSM_Moments_NoDepAtWork_compare.dta, replace;
};

preserve;
    keep if t==50;
    keep model t alfpr1to0 alfpr0to1 plnw41;
    sort model;
    export excel model alfpr1to0 alfpr0to1 plnw41 using "table4paper_NoDepAtWork", 
       sheet("transitions") sheetreplace firstrow(variables);
restore;

local lvmoments lfpr lnw sdlnw lnw_fd alfpr1to0 alfpr0to1 C ssa plnw41 `lvsdms';

keep t model `lvmoments';
reshape wide `lvmoments', i(t) j(model);

sort t;
save ${gdtadir}/dta_MSM_Moments_NoDepAtWork_compare.dta, replace;

use dta_MSM_ProfilesMoments.dta if ihet==0, clear;
drop ihet;
drop lfpr1 lfpr2 lfpr3 lfpr4 lnw1 lnw2 lnw3 lnw4; // different health status;
sort t;
merge 1:1 t using ${gdtadir}/dta_MSM_Moments_NoDepAtWork_compare.dta, nogenerate;
sort t;
save ${gdtadir}/dta_MSM_Moments_NoDepAtWork_compare.dta, replace;

sort t;

* plot graph;

foreach iv in lfpr lnw sdlnw lnw_fd C ssa {;
   local lyange "";
   local lylabel "";
   if ("`iv'"=="lfpr_diff") {;
      local lvtperiod if t>=${gvdata_diff} & t<=${gvdata10};
      local lvxlabel "xlabel(${gvdata_diff}(5)60 62 ${gvdata10},grid)";
      local lvytitle "Difference in LFPR";
      local lvpos = 6;
   };
   else if ("`iv'"=="ssa") {;
      local lvtperiod if t>=${gvdata_ss} & t<=${gvdata10_ss};
      local lvxlabel "xlabel(${gvdata_ss}(1)${gvdata10_ss},grid)";
      local lvytitle "Social Security Application";
      local lvpos = 1;
   };

   else {;
      local lvtperiod "if t>=${gvdata0} & t<=${gvdata10}";
      local lvxlabel "xlabel(${gvdata0} 30(10)60 62 ${gvdata10},grid)";
      if ("`iv'"=="lfpr") {;
         local lvytitle "Labor Force Participation Rates";
         local lvpos = 7;
      };
      else if ("`iv'"=="lnw") {;
         local lvytitle "Log Wages";
         local lvpos = 6;
      };
      else if ("`iv'"=="lnw_fd") {;
         local lvytitle "Log Wages (FD)";
         local lvpos = 6;
      };
      else if ("`iv'"=="sdlnw") {;
         local lvytitle "Log Wages: Standard Deviation";
         local lvpos = 11;
         local lyange 0 1;
         local lylabel 0(0.2)1;
      };
      else if ("`iv'"=="C") {;
         local lvytitle "Consumption";
         local lvpos = 6;
         local lyange 4 12;
         local lylabel 4(2)12;
      };
   };

   line `iv'_d `iv'1 `iv'2 t `lvtperiod', lpattern("l" "_" "_." "-" "-." "__--" "_.-." ) graphregion(color(white))
        `lvxlabel' ytitle("`lvytitle'") yscale(range(`lyrange')) ylabel(`lylabel')
        legend(pos(`lvpos') ring(0) col(1) order(1 "Data" 2 "Version 1" 3 "Version 2"));
   graph export ${gpicsdir}/eps_moments_NoDepAtWork_compare_`iv'.eps,replace;
};


foreach iv in sdH sdA sdinc p50A A {;
   if inlist("`iv'","A","sdA","sdAIME","p1A","p2A","p5A") | inlist("`iv'","p50A","p95A","p98A","p99A","p50lnw","p50inc") {;
       local lvpos = 11;
   };
   else if inlist("`iv'","sdH","sdC","sdinc") {;
       local lvpos = 6;
   };
   else if inlist("`iv'","corr_lfpr_H") {;
       local lvpos = 5;
   };
   else if inlist("`iv'","lfpr1to0") {;
       local lvpos = 11;
   };
   else {;
       local lvpos = 7;
   };
   line `iv' `iv'1 `iv'2 t, // if t>=${gvdata0} & t<=${gvdata10}, 
        lpattern("l" "_" "_." "-" "-." "__--" "_.-." "_.-" "_.." "-.." ) graphregion(color(white))
        // xlabel(${gvdata0} 30(10)60 62 ${gvdata10},grid) 
        ytitle("`iv'") 
        legend(pos(`lvpos') ring(0) col(1) order(1 "Baseline" 2 "NoDep v1" 3 "NoDep v2"));
   graph export ${gpicsdir}/eps_BvsNoDep_`iv'.eps,replace;
};

local lvcond65 if t>=22 & t<=65;
local lvxlabel65 "xlabel(22 30(10)60 65,grid)";
local lvlegr0col1 colgap(*0.75) keygap(*0.5) symxsize(*0.9) symysize(*0.75);
line I I1 I2 t `lvcond65', lpattern("l" "_" "_." "-" "-." "__--") 
     ytitle("Investment") graphregion(color(white)) 
     yscale(range(0 0.15)) ylabel(0(0.05)0.20) `lvxlabel65' 
     legend(pos(2) ring(0) col(1) `lvlegr0col1' order(1 "Baseline" 2 "NoDep v1" 3 "NoDep v2"));
graph export ${gpicsdir}/eps_BvsNoDep_I_65.eps,replace;

line lnH lnH1 lnH2 t `lvcond65', lpattern("l" "_" "_." "-" "-." "__--")
     ytitle(${gytitle_lnw}) graphregion(color(white)) `lvxlabel65' 
     legend(pos(6) ring(0) col(1) `lvlegr0col1' order(1 "Baseline" 2 "NoDep v1" 3 "NoDep v2"));
graph export ${gpicsdir}/eps_BvsNoDep_lnH_65.eps,replace;


cap log close;

*  keygap(*1) symxsize(*1);


